package ch.mtb.conwin;

import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

/**
 * Sample application using Frame.
 * 
 * @author
 * @version 1.00 05/01/18
 */
public class ConWinFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Population _population;

    private Thread _t;

    public void startMe() {
        _t = new Thread(_population);
        _t.start();
    }
    
    public void stopMe() {
        _t.stop();
    }

    /**
     * The constructor.
     */
    public ConWinFrame() {
        _population = new Population();
        getContentPane().add(_population);
        MenuBar menuBar = new MenuBar();
        Menu menuFile = new Menu("File");
        MenuItem menuFileExit = new MenuItem("Exit");

        // Add action listener.for the menu button
        menuFileExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ConWinFrame.this.windowClosed();
            }
        });
        menuFile.add(menuFileExit);
        menuBar.add(menuFile);

        Menu menuRun = new Menu("Run");

        final MenuItem start = new MenuItem("Start");
        final MenuItem stop = new MenuItem("Stop");
        final MenuItem torus = new MenuItem("Torus");
        final MenuItem square = new MenuItem("Square");
        
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                torus.setEnabled(false);
                square.setEnabled(false);
                startMe();
            }
        });
        
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(true);
                //torus.setEnabled(false);
                square.setEnabled(true);
                stopMe();
            }
        });
        
        menuRun.add(start);
        menuRun.add(stop);
        menuBar.add(menuRun);

        Menu menuOptions = new Menu("Options");

        torus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getContentPane().remove(_population);
                _population = new TorusPopulation();
                getContentPane().add(_population);
                getContentPane().validate();
                torus.setEnabled(false);
                square.setEnabled(true);
            }
        });
        square.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getContentPane().remove(_population);
                _population = new Population();
                getContentPane().add(_population);
                getContentPane().validate();
                square.setEnabled(false);
                torus.setEnabled(true);
            }
        });
        square.setEnabled(false);
        menuOptions.add(torus);
        menuOptions.add(square);
        menuBar.add(menuOptions);

        setTitle("Darwins Life");
        setMenuBar(menuBar);
        // setSize(new Dimension((population.getDimensions())*(width + 1),
        // population.getDimensions() * width));

        // Add window listener.
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                ConWinFrame.this.windowClosed();
            }}
        ); 
        
     
        
   //    Show frame
  	     this.setVisible(true); 
    }

    /**
     * Shutdown procedure when run as an application.
     */
    protected void windowClosed() {

        // TODO: Check if it is safe to close the application

        // Exit application.
        System.exit(0);
    }

}
